<?php
header('Access-Control-Allow-Origin:*'); //注意！跨域要加这个头 
require_once "./conn.php";

// 获取前端传来的 JSON 数据
$jsonArray = file_get_contents('php://input');

// 解析JSON数据
$jsonArray = json_decode($jsonArray, TRUE);

if (json_last_error() !== JSON_ERROR_NONE) {
    die("Invalid JSON format");
}

// 打印解析后的JSON数据（调试用）
print_r($jsonArray);

// 提取JSON数据中的字段
$id = isset($jsonArray['id']) ? intval($jsonArray['id']) : null;
$user_device_name = isset($jsonArray['user_device_name']) ? $jsonArray['user_device_name'] : '';
$user_account = isset($jsonArray['user_account']) ? $jsonArray['user_account'] : '';
$user_password = isset($jsonArray['user_password']) ? $jsonArray['user_password'] : '';
$user_zone_name = isset($jsonArray['user_zone_name']) ? $jsonArray['user_zone_name'] : '';
$user_name = isset($jsonArray['user_name']) ? $jsonArray['user_name'] : '';
$user_role = isset($jsonArray['user_role']) ? $jsonArray['user_role'] : '';
$user_level = isset($jsonArray['user_level']) ? $jsonArray['user_level'] : '';
$user_id = isset($jsonArray['user_id']) ? $jsonArray['user_id'] : '';
$user_sect = isset($jsonArray['user_sect']) ? $jsonArray['user_sect'] : '';
$user_gold_coin = isset($jsonArray['user_gold_coin']) ? floatval($jsonArray['user_gold_coin']) : 0;
$user_silver_coin = isset($jsonArray['user_silver_coin']) ? floatval($jsonArray['user_silver_coin']) : 0;
$user_activity = isset($jsonArray['user_activity']) ? intval($jsonArray['user_activity']) : 0;
$user_status = isset($jsonArray['user_status']) ? $jsonArray['user_status'] : '';
$user_run_time = isset($jsonArray['user_run_time']) ? $jsonArray['user_run_time'] : '';
$user_last_time = isset($jsonArray['user_last_time']) ? $jsonArray['user_last_time'] : '';
$comment = isset($jsonArray['comment']) ? $jsonArray['comment'] : '';
$user_gem_hammer = isset($jsonArray['user_gem_hammer']) ? intval($jsonArray['user_gem_hammer']) : 0;
$frozen_gold_coin = isset($jsonArray['frozen_gold_coin']) ? floatval($jsonArray['frozen_gold_coin']) : 0.00;
$gold_coin_growth = 0.00; // 初始化金币涨幅为0

// 判断有没有这个账号
$user_zhongkong_name = isset($jsonArray['user_zhongkong_name']) ? $jsonArray['user_zhongkong_name'] : '';

// 查询是否存在该中控账号
$sql0 = "SELECT uname FROM users WHERE uname = ?";
$stmt0 = $conn->prepare($sql0);
$stmt0->bind_param("s", $user_zhongkong_name);
$stmt0->execute();
$result0 = $stmt0->get_result();
$row0 = $result0->fetch_assoc();

if ($row0) {
    // 查询是否存在该用户的上一次金币数量
    $sql1 = "SELECT user_gold_coin FROM roles WHERE user_name = ? ORDER BY update_time DESC LIMIT 1";
    $stmt1 = $conn->prepare($sql1);
    $stmt1->bind_param("sssss", $user_account, $user_device_name, $user_password, $user_zone_name, $user_zhongkong_name);
    $stmt1->execute();
    $result1 = $stmt1->get_result();
    $row1 = $result1->fetch_assoc();

    if ($row1) {
        $previous_gold_coin = floatval($row1['user_gold_coin']);
        if (isset($user_gold_coin)) {
            $gold_coin_growth = $user_gold_coin - $previous_gold_coin;
        }
    }

    // 插入或更新记录
    $sql = "REPLACE INTO roles(id, user_zhongkong_name, user_device_name, user_account, user_password, user_zone_name, user_name, user_role, user_level, user_id, user_sect, user_gold_coin, user_silver_coin, user_activity, user_status, user_run_time, user_last_time, update_time, comment, user_gem_hammer, frozen_gold_coin, gold_coin_growth) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("issssssssssssssssssss", $id, $user_zhongkong_name, $user_device_name, $user_account, $user_password, $user_zone_name, $user_name, $user_role, $user_level, $user_id, $user_sect, $user_gold_coin, $user_silver_coin, $user_activity, $user_status, $user_run_time, $user_last_time, $comment, $user_gem_hammer, $frozen_gold_coin, $gold_coin_growth);

    if ($stmt->execute()) {
        echo "提交成功";
    } else {
        echo "提交失败,失败原因：" . $stmt->error;
    }
} else {
    echo '此中控账号不存在,请前往1.13.198.191:8081注册';
}